<?php

/* @var $this yii\web\View */

$this->title = '玩客快栈';
?>
<div class="site-index">

<?php echo $this->render('/public/top'); ?>
<?php if(Yii::$app->user->isGuest):?>
    <form>
        <fieldset>
            <label>手机号</label>
            <input type="text" id="mobile-login" placeholder="请输入手机号…">
            <span class="help-block"></span>

            <label>密码</label>
            <input type="password" id="password-login" placeholder="请输入密码…">
            <span class="help-block"></span>

            <input type="hidden" value="<?php echo \Yii::$app->getRequest()->getCsrfToken(); ?>" id="YII_CSRF_TOKEN2" name="YII_CSRF_TOKEN" />
            <button type="submit" class="btn btn-info bt-login">登录</button>
            <a href="/site/register" class="btn">注册</a>
        </fieldset>
    </form>
<?php else:?>
    <h2>我的宠物</h2>
    <div class="col-lg-1">
        <?php if(!empty($userPetRow)):?>
        <?php foreach ($userPetRow as $userPetItem):?>
            <?php
                $feedToday = $userPetItem->getSumFee();
                $feedPercent = bcdiv($feedToday, 5,2) * 100;
                $noPercent = (1 - bcdiv($feedToday, 5,2)) * 100;
             ?>
            <div class="col-lg-6" style="float: left; width: 100%; margin-top: 10px;">
                <div style="float:left;width: 27%">
                    <a href="/pet/detail?pid=<?php echo $userPetItem->pet->pid?>">
                    <img src="/images/pig/p<?php echo $userPetItem->pet->style?>.svg" width="150" class="pig-bg<?php echo $userPetItem->pet->getPetBack()?>">
                    </a>
                </div>
                <div style="float: left; padding-left: 8px">
                    <p><?php echo (int) $userPetItem->pet->algebra?> 代 · <?php echo $userPetItem->pet->height?> kg</p>
                    <p>成长: <?php echo $userPetItem->pet->grow?></p>
                    <p>掘金：<?php echo $userPetItem->pet->nuggets?></p>
                    <span data-v-59eaeb2c="">每投喂 1wkc 体重增长 <?php echo bcdiv($userPetItem->pet->nuggets, 1, 3)?>kg</span><br><br>
                    <div class="progress" style="height: 15px;font-size: 11px">
                        <div class="bar bar-success" style="font-size:11px;line-height:15px;width: <?php echo $feedPercent;?>%;">已喂食<?php echo (int) $feedToday?></div>
                        <div class="bar bar-danger" style="font-size:11px;line-height:15px;width: <?php echo $noPercent;?>%;">未喂食<?php echo (int) (5-$feedToday)?></div>
                    </div>
                    <p>
                        <?php if($userPetItem->auction_status == 0):?>
                        <a href="#sailShow<?php echo $userPetItem->pid?>" role="button" class="btn" data-toggle="modal">挂卖</a>
                        <?php else:?>
                            <span><font style="color: #2f96b4;font-weight: bold;font-size: 16px"><?php echo $userPetItem->price?></font> wkc</span>
                            <a href="javascript:void(0)" class="btn btn-hang-cancel" pid="<?php echo $userPetItem->pid?>">取消挂卖</a>
                        <?php endif?>
                    </p>
                </div>
                <div class="modal hide fade" id="sailShow<?php echo $userPetItem->pid?>">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h3>我要挂卖</h3>
                    </div>
                    <div class="modal-body">
                        <p>
                            <input type="text" placeholder="请输入售卖价格…" style="width: 120px">
                        </p>
                        <a class="btn btn-success btn-hang" pid="<?php echo $userPetItem->pid?>">挂卖</a>
                        <p style="color: #969696;padding:5px 10px">
                            挂卖过程中，如果被其它玩家购买，系统将从您售卖价格中收取您5%的手续费
                        </p>
                    </div>
                    <div class="modal-footer">
                        <a href="#" class="btn" data-dismiss="modal">关闭</a>
                    </div>
                </div>
            </div>
            <?php endforeach;?>
        <?php endif;?>
    </div>
<?php endif;?>
    <script type="text/javascript" src="/js/site_user2.js"></script>
    <script type="text/javascript">
        var token = '<?php echo \Yii::$app->getRequest()->getCsrfToken(); ?>';

        $(document).ready(function () {
            //挂卖
            $('.btn-hang').click(function () {
                var price = $(this).prev().children().val();
                if (price <= 0) {
                    alert('售卖价格必须大于0');
                    return false;
                }
                var pid = $(this).attr('pid');
                $.post('/pet/hang', {pid:pid,price:price, _csrf:token}, function (data) {
                    if (data.code == 10000) {
                        location.reload();
                        return true;
                    }
                    alert(data.msg);
                    return false;
                });
            });

            //取消挂卖
            $('.btn-hang-cancel').click(function () {
                var pid = $(this).attr('pid');
                $.post('/pet/cancel-hang', {pid:pid, _csrf:token}, function (data) {
                    if (data.code == 10000) {
                        location.reload();
                        return true;
                    }
                    alert(data.msg);
                    return false;
                });
            });
        });
    </script>
</div>
